# #!/usr/bin/env python
# # -*- coding: utf-8 -*-
# import os.path
# # here put the import lib
# from distutils.core import setup
# from distutils.extension import Extension
#
# from Cython.Build import cythonize
#
# files_address = 'Utils'
# selected_string = '.py'
#
#
# def find_py(path='.'):
#     py_files = []
#     for root, dirs, files in os.walk(path):
#         for file in files:
#             if file.endswith(selected_string):
#                 full_path = os.path.join(root, file)
#                 py_files.append(full_path)
#     return py_files
#
#
# # ext_modules = [
# #     "config.py",
# #     "main.py",
# #     "multi_team.py",
# #     "multi_team_parallel.py",
# #     "task_runner.py"
# # ]
#
# ext_modules = find_py(files_address)
#
# extensions = [
#     Extension(
#         os.path.splitext(os.path.relpath(f, files_address))[0].replace(os.path.sep, "."),
#         [f]
#     ) for f in ext_modules
# ]
#
# ext_modules = [
#     "Utils",
#     # "AlgorithmBase.py",
#     # "cssim_env_wrapper.py",
#     # "CssimAgentEnvBase.py",
# ]
#
# setup(ext_modules=cythonize(ext_modules))

# 代码在命令行执行：python setup.py build_ext --inplace
# 生成.c和.pyd文件，其中.pyd文件要去除前面的python版本后缀才能使用
#


import os
from distutils.core import setup
from Cython.Build import cythonize

def compile_py_to_pyd(directory):
    # 遍历指定目录下的所有文件
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith(".py"):
                py_file_path = os.path.join(root, file)
                # 使用Cython编译.py文件为.pyd文件
                setup(
                    ext_modules=cythonize(py_file_path),
                    script_args=["build_ext", "--inplace"],
                )
                # 删除生成的.c文件和build目录
                base_name = os.path.splitext(py_file_path)[0]
                os.remove(base_name + ".c")
                for build_dir in ["build"]:
                    if os.path.exists(build_dir):
                        for build_file in os.listdir(build_dir):
                            os.remove(os.path.join(build_dir, build_file))
                        os.rmdir(build_dir)

if __name__ == "__main__":
    # 指定要编译的目录
    target_directory = "Utils"
    compile_py_to_pyd(target_directory)